package com.zhouyu;

import com.zhouyu.aop.MuseAop;
import com.zhouyu.lifeCycle.LifeCycleAppConfig;
import com.zhouyu.lifeCycle.TestLifeCycle;
import com.zhouyu.service.UserService;
import com.zhouyu.testCircularDep.App2Config;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
//		testCircularDep();

//		test();

//		aop();

		// 测试bean的生命周期
		lifeCycle();

	}

	/**
	 * 测试aop
	 */
	public static void aop() {
		// 创建一个Spring容器
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
		MuseAop museAop = (MuseAop) context.getBean("museAop");
		museAop.goToWork();
		System.out.println("end……");
	}

	/**
	 * 测试bean的生命周期
	 */
	public static void close() {
		// 创建一个Spring容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleAppConfig.class);
		TestLifeCycle testLifeCycle = (TestLifeCycle) applicationContext.getBean("testLifeCycle");
		// 注册一个关闭钩子，在jvm关闭的时候，会调用所有已经注册的关闭钩子
		applicationContext.registerShutdownHook();
		System.out.println("end……");
		applicationContext.close();

	}

	/**
	 * 测试bean的生命周期
	 */
	public static void lifeCycle() {
		// 创建一个Spring容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(LifeCycleAppConfig.class);
		TestLifeCycle testLifeCycle = (TestLifeCycle) applicationContext.getBean("testLifeCycle");
		System.out.println("end……");
	}

	/**
	 * 测试循环依赖
	 */
	public static void testCircularDep() {
		// 创建一个Spring容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(App2Config.class);
//		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
		System.out.println("end……");


	}

	/**
	 * 测试生命周期
	 */
	public static void test() {
		// 创建一个Spring容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

		UserService userService = (UserService) applicationContext.getBean("userService");
		userService.test();
	}
}







